package com.artisan.admin.controllers;

import com.artisan.admin.model.regionViewModels.RegionViewModel;
import com.artisan.admin.requestModels.accountListRequestModels.AccountListRequestModel;
import com.artisan.admin.resultModels.MessageBase;
import com.artisan.admin.resultModels.accountResultModels.AccountResultModel;
import com.artisan.admin.resultModels.regionResultModels.RegionResultModel;
import com.artisan.domain.dto.AccountDto;
import com.artisan.domain.dto.RegionDto;
import com.artisan.domain.serviceInterface.IRegionService;
import com.artisan.util.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/** 地区控制器 */
@RequestMapping("/api/region")
@RestController
@Api("地区模块")
@ApiOperation(value = "地区模块")
public class RegionController {
  @Autowired private IRegionService regionService;

  /** 获得列表 */
  // GET /list
  @ApiOperation(value = "获得列表")
  @ApiResponses(@ApiResponse(code = 200, response = RegionResultModel.class, message = "成功"))
  @RequestMapping(
      value = {"/list"},
      method = {RequestMethod.GET})
  public MessageBase getList() {
    MessageBase result = new MessageBase();
    List<RegionDto> dtos = this.regionService.findList();

    ArrayList<RegionResultModel> models = new ArrayList<>();

    dtos.forEach(
        dto -> {
          RegionResultModel model = new RegionResultModel();
          BeanUtils.copyProperties(dto, model);

          models.add(model);
        });

    result.setData(models);
    return result;
  }

  /**
   * 添加
   *
   * @param model 模型
   */
  // POST /
  @ApiOperation(value = "添加")
  @ApiResponses(@ApiResponse(code = 200, response = RegionResultModel.class, message = "成功"))
  @RequestMapping(
      value = {"/"},
      method = {RequestMethod.POST})
  public MessageBase post(@RequestBody RegionViewModel model) {
    MessageBase result = new MessageBase();
    RegionDto dto = new RegionDto();
    BeanUtils.copyProperties(model, dto);
    int id = this.regionService.add(dto);
    result.setSuccess(id > 0);
    return result;
  }

  /**
   * 获得详情
   *
   * @param id id
   */
  // GET /{1}
  @ApiOperation(value = "获得详情")
  @ApiResponses(@ApiResponse(code = 200, response = RegionResultModel.class, message = "成功"))
  @RequestMapping(
      value = {"/{id}"},
      method = {RequestMethod.GET})
  public MessageBase get(@PathVariable int id) {
    MessageBase result = new MessageBase();
    RegionDto dto = this.regionService.find(id);
    RegionResultModel model = new RegionResultModel();
    BeanUtils.copyProperties(dto, model);
    result.setData(model);
    return result;
  }

  /**
   * 更新
   * @param id id
   * @param model 模型
   */
  // PUT /{1}
  @ApiOperation(value = "更新")
  @ApiResponses(@ApiResponse(code = 200, response = RegionResultModel.class, message = "成功"))
  @RequestMapping(
          value = {"/{id}"},
          method = {RequestMethod.PUT})
  public MessageBase put(@PathVariable int id, @RequestBody RegionViewModel model) {
    MessageBase result = new MessageBase();
    RegionDto dto = new RegionDto();
    BeanUtils.copyProperties(model, dto);
    dto.setId(id);
    boolean b = this.regionService.update(dto);
    result.setSuccess(b);
    return result;
  }

  /**
   * 删除数据
   * @param id id
   */
  // DELETE /{1}
  @ApiOperation(value = "删除数据")
  @ApiResponses(@ApiResponse(code = 200, response = RegionResultModel.class, message = "成功"))
  @RequestMapping(
          value = {"/{id}"},
          method = {RequestMethod.DELETE})
  public MessageBase delete(@PathVariable int id) {
    MessageBase result = new MessageBase();
    boolean b = this.regionService.delete(id);
    result.setSuccess(b);
    return result;
  }
}
